package com.yyx.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyx.common.R;
import com.yyx.entity.Category;
import com.yyx.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author yeyinxuan
 * @since 2023-07-31
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 保存菜单分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        Boolean b = categoryService.saveCategory(category);
        if(!b){
            return R.error("添加分类失败");
        }
        return R.success("添加分类成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        Page<Category> c = categoryService.selectpage(page,pageSize);
        return R.success(c);
    }

    @DeleteMapping
    public R delectbyids(Long ids){
        boolean b = categoryService.delcetCategoryByid(ids);
            return R.success("删除成功");
    }
    @PutMapping
    public R update(@RequestBody Category category){
        Boolean b = categoryService.updateById(category);
        if(!b){
            return R.error("该分类已存在，修改失败");
        }
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R list(Category category){
        List list = categoryService.listType(category);
        return R.success(list);
    }

}
